package com.kejizhentan.cmn.controller;

import com.kejizhentan.cmn.service.DictService;
import com.kejizhentan.common.result.Result;
import com.kejizhentan.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    // 根据 dictcode 和 value 查询
    @ApiOperation(value = "根据 dictcode 和 value 查询")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        return dictService.getDictName(dictCode, value);
    }

    // 根据 value 查询
    @ApiOperation(value = "根据 value 查询")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value) {
        return dictService.getDictName("", value);
    }

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }

    /**
     * @Auther: kejizhentan
     * @Date 2022/11/27 12:26
     * @Description: 导出数据字典
     */
    @ApiOperation(value = "导出数据字典")
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response) throws Exception {
        dictService.exportDict(response);
    }


    /**
     * @Auther: kejizhentan
     * @Date 2022/11/27 13:05
     * @Description: 导入数据字典
     */
    @ApiOperation(value = "导入数据字典")
    @PostMapping("/importData")
    public Result importDict(MultipartFile file) {
        dictService.importDict(file);
        return Result.ok();
    }


    // 根据 dictCode 获取下级节点
    @ApiOperation(value = "根据 dictCode 获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}